from urllib import request,parse
import json


def interprete(keyword):
    
    url="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"

    #定义请求参数,编码转换
    data = {"i":keyword,'doctype':'json'}
    data= parse.urlencode(data)



    #创建请求 封装对象
    req= request.Request(url,data=bytes(data,encoding="utf-8"))

    #抓取数据，打开req
    res = request.urlopen(req)

    #解析结果
    str_json=res.read().decode("utf-8")
    myjson=json.loads(str_json)

    print(myjson['translateResult'][0][0]['tgt'])


if __name__=="__main__":
    while True:
        keyword=input('请输入要翻译的单词')
        if keyword == 'q':
            break
        interprete(keyword)
